package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.approvetask.entity.CaseApproveRecord;
import com.ruicar.afs.cloud.afscase.approvetask.entity.WorkProcessScheduleInfo;
import com.ruicar.afs.cloud.afscase.approvetask.service.CaseApproveRecordService;
import com.ruicar.afs.cloud.afscase.approvetask.service.WorkProcessScheduleInfoService;
import com.ruicar.afs.cloud.afscase.common.step.StepParam;
import com.ruicar.afs.cloud.afscase.common.utils.StepUtil;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.*;
import com.ruicar.afs.cloud.afscase.infomanagement.service.*;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.service.ApproveInformInfoService;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.vo.CaseLogInfo;
import com.ruicar.afs.cloud.afscase.processor.enums.LoanSubmitEnum;
import com.ruicar.afs.cloud.afscase.processor.enums.WorkflowType;
import com.ruicar.afs.cloud.afscase.step.entity.CaseStepParam;
import com.ruicar.afs.cloud.afscase.step.service.CaseStepParamService;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinCostDetails;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinDiscountDetails;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinFinancingItems;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinRentAdjustDetails;
import com.ruicar.afs.cloud.bizcommon.business.service.ApplyRentAdjustDetailsService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.*;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.*;
import com.ruicar.afs.cloud.common.modules.enums.ApplySceneEnum;
import com.ruicar.afs.cloud.common.modules.enums.OprTypeEnum;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

/**
 * @description:进件-->案件MQ
 * @author jiaqi.guo
 * @created 2020/7/28 12:57
 * @version 1.0
 */
@AllArgsConstructor
@Slf4j
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class CaseDealProcessor implements AfsMqBizProcessor<OrderSubmitInfo> {

    private CaseBaseInfoService caseBaseInfoService;

    private CaseCustContactService caseCustContactService;

    private CaseApproveRecordService caseApproveRecordService;

    private CaseCarInfoService caseCarInfoService;

    private CaseCostInfoService caseCostInfoService;

    private CaseCustAddressService caseCustAddressService;

    private CaseCustCompanyService caseCustCompanyService;

    private CaseCustIndividualService caseCustIndividualService;

    private CaseCustInfoService caseCustInfoService;

    private CaseDiscountDetailService caseDiscountDetailService;

    private CaseFinancingItemsService caseFinancingItemsService;

    private CaseChannelInfoService caseChannelInfoService;

    private CaseCarStyleDetailService caseCarStyleDetailService;

    private ApproveInformInfoService approveInformInfoService;

    private ApplyRentAdjustDetailsService applyRentAdjustDetailsService;

    private ComAttachmentFileService comAttachmentFileService;

    private CaseRedundantInfoService redundantInfoService;

    private CaseStepParamService caseStepParamService;

    private final WorkProcessScheduleInfoService workProcessScheduleInfoService;

    private CaseFinRentAdjustDetailsService  rentAdjustDetailsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(OrderSubmitInfo entity) {
        /**  2.1申请编号  **/
        String applyNo = entity.getApplyNo();
        CaseLogInfo logInfo = new CaseLogInfo();
        logInfo.setApplyNo(applyNo);
        logInfo.setResult(ResultBooleanEnum.TRUE.getCode());
        Throwable throwable = null;
        try {
            /**申请场景-常规审批*/
            if (ApplySceneEnum.GENERALAPPROVAL.getCode().equals(entity.getApplyScene())) {
                this.receiveSubmitCaseInfo(entity);
            }
            /**正式复议*/
            if(ApplySceneEnum.FORMALREVIEW.getCode().equals(entity.getApplyScene())){
                this.receiveSubmitCaseInfo(entity);
            }
        } catch (Throwable e) {
            throwable = e;
            logInfo.setResult(ResultBooleanEnum.FALSE.getCode());
            log.info("异常原因：{}", e.getMessage());
        }
        approveInformInfoService.incomingApplyResult(logInfo);
        log.info("MQ日志保存成功-结果：{}", logInfo.getResult());
        log.info("报文信息：{}", entity.toString());
        if (ResultBooleanEnum.FALSE.getCode().equals(logInfo.getResult())) {
            throw new AfsBaseException("", "保存失败!", throwable);
        }
        // 分单前置逻辑
        if (ResultBooleanEnum.TRUE.getCode().equals(logInfo.getResult())) {
            CaseStepParam caseStepParam = caseStepParamService.getLast(applyNo,StepSceneEnum.APPROVE);
            StepParam param = StepParam.builder().applyNo(applyNo).build();
            if (ApplySceneEnum.GENERALAPPROVAL.getCode().equals(entity.getApplyScene())
                    && OprTypeEnum.OPRUPDATE.getCode().equals(entity.getOprType())) {
                // 流程中退回重提
                if(StrUtil.isNotBlank(entity.getDataId()) && !WhetherEnum.YES.getCode().equals(entity.getIsCallBackSubmit())) {
                    param.setStageId(entity.getDataId());
                    param.setReturnSubmit(true);
                    param.setProcessTypeEnum(ProcessTypeEnum.GENERA_APPROVE);

                    caseStepParam.setStageId(entity.getDataId());
                    caseStepParam.setFlowType(ProcessTypeEnum.GENERA_APPROVE);
                    caseStepParam.setReturnSubmit(WhetherEnum.YES.getCode());
                    log.info("流程中退回重提{}", entity.getDataId());
                }
                // 流程中撤回重提 add by maxueyu
                else if(WhetherEnum.YES.getCode().equals(entity.getIsCallBackSubmit())) {
                    param.setStageId(entity.getDataId());
                    param.setCallBackSubmit(true);
                    param.setProcessTypeEnum(ProcessTypeEnum.GENERA_APPROVE);
                    caseStepParam.setStageId(entity.getDataId());
                    caseStepParam.setFlowType(ProcessTypeEnum.GENERA_APPROVE);
                    log.info("流程中撤回重提，申请编号{}", entity.getApplyNo());
                }
            }else if(ApplySceneEnum.FORMALREVIEW.getCode().equals(entity.getApplyScene())) {
                caseStepParam.setFlowType(ProcessTypeEnum.FORMAL_REVIEW);
                param.setProcessTypeEnum(ProcessTypeEnum.FORMAL_REVIEW);
                if (OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(entity.getOprType())) {
                    caseStepParam.setStageId(entity.getDataId());
                    caseStepParam.setReturnSubmit(WhetherEnum.YES.getCode());

                    param.setReturnSubmit(true);
                    param.setStageId(entity.getDataId());
                    log.info("正式复议修改流程{}", entity.getDataId());
                }
            }
            /**
             * 修订回复留言
             */
            if(StringUtils.isNotEmpty(entity.getLeaveMessage())){
                caseStepParam.setLeaveMessage(entity.getLeaveMessage());
                param.setLeaveMessage(entity.getLeaveMessage());
            }

            caseStepParam.setScene(StepSceneEnum.APPROVE);
            caseStepParam.setApplyNo(applyNo);
            caseStepParamService.saveOrUpdate(caseStepParam);
            StepUtil.prevSteps(param);
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_APPLY_CASE_CTM_NEW_ORDER;
    }

    /**
     * receiveSubmitCaseInfo
     * <p>Description: The Listener of /order/submitCaseInfo </p>
     *
     * @param orderSubmitInfo Serializable Object
     * @return
     */

    public void receiveSubmitCaseInfo(OrderSubmitInfo orderSubmitInfo) throws ParseException {
        /**新增操作附条件核准冗余数据*/
        JSONObject object=new JSONObject();
        /**新增操作退回留痕冗余数据*/
        JSONObject jsonObject=new JSONObject();
        if (OprTypeEnum.OPRADD.getCode().equals(orderSubmitInfo.getOprType())) {
            log.info("------------------------------常规审批流程新增操作start----------------------------");
            /**  2.2解析客户联系方式CaseCustContact  **/
            ArrayList<CaseCustContact> caseCustContactArrayList = this.parseCaseCustContact(orderSubmitInfo);
            caseCustContactService.saveBatch(caseCustContactArrayList, caseCustContactArrayList.size());
            log.info("客户联系方式add成功！！！");


            /**  2.3解析车辆信息CaseCarInfo  **/
            ArrayList<CaseCarInfo> caseCarInfoArrayList = this.parseCaseCarInfo(orderSubmitInfo);
            caseCarInfoService.saveBatch(caseCarInfoArrayList, caseCarInfoArrayList.size());
            log.info("车辆信息add成功！！！");


            /**  2.4解析融资信息CaseCostInfo  **/
            ArrayList<FinCostDetails> finCostInfoArrayList = this.parseCaseCostInfo(orderSubmitInfo);
            caseCostInfoService.saveBatch(finCostInfoArrayList, finCostInfoArrayList.size());
            log.info("融资信息add成功！！！");


            /**  2.5解析客户地址明细CaseCustAddress  **/
            ArrayList<CaseCustAddress> caseCustAddressArrayList = this.parseCaseCustAddress(orderSubmitInfo);
            caseCustAddressService.saveBatch(caseCustAddressArrayList, caseCustAddressArrayList.size());
            log.info("客户地址明细add成功！！！");


            /**  2.6解析企业客户明细信息CaseCustCompany  **/
            ArrayList<CaseCustCompany> caseCustCompanyArrayList = this.parseCaseCustCompany(orderSubmitInfo);
            caseCustCompanyService.saveBatch(caseCustCompanyArrayList, caseCustCompanyArrayList.size());
            log.info("企业客户明细信息add成功！！！");

            /**  2.7解析个人客户明细信息CaseCustIndividual  **/
            ArrayList<CaseCustIndividual> caseCustIndividualArrayList = this.parseCaseCustIndividual(orderSubmitInfo);
            caseCustIndividualService.saveBatch(caseCustIndividualArrayList, caseCustIndividualArrayList.size());
            log.info("个人客户明细信息add成功！！！");



            /**  2.8解析客户信息CaseCustInfo  **/
            ArrayList<CaseCustInfo> caseCustInfoArrayList = this.parseCaseCustInfo(orderSubmitInfo);
            caseCustInfoService.saveBatch(caseCustInfoArrayList, caseCustInfoArrayList.size());
            log.info("客户信息add成功！！！");


            /**  2.9解析贴息明细信息CaseDiscountDetail  **/
            ArrayList<FinDiscountDetails> caseDiscountDetailArrayList = this.parseCaseDiscountDetail(orderSubmitInfo);
            caseDiscountDetailService.saveBatch(caseDiscountDetailArrayList, caseDiscountDetailArrayList.size());
            log.info("贴息明细信息add成功！！！");


            /**  2.10解析融资项目信息CaseFinancingItems  **/
            ArrayList<FinFinancingItems> finFinancingItemsArrayList = this.parseCaseFinancingItems(orderSubmitInfo);
            caseFinancingItemsService.saveBatch(finFinancingItemsArrayList, finFinancingItemsArrayList.size());
            log.info("融资项目信息add成功！！！");


            /**  2.11解析案件渠道信息CaseChannelInfo  **/
            CaseChannelInfo caseChannelInfo = this.parseCaseChannelInfo(orderSubmitInfo);
            caseChannelInfoService.save(caseChannelInfo);
            log.info("案件渠道信息add成功！！！");

            /**  2.12解析车辆信息CaseCarStyleDetail  **/
            ArrayList<CaseCarStyleDetail> caseCarStyleDetailArrayList = this.parseCaseCarStyleDetail(orderSubmitInfo);
            caseCarStyleDetailService.saveBatch(caseCarStyleDetailArrayList, caseCarStyleDetailArrayList.size());
            log.info("车辆信息add成功！！！");

            /**  2.13解析租金调整ApplyRentAdjustDetails  **/
            ArrayList<FinRentAdjustDetails> finRentAdjustDetailsArrayList = this.parseApplyRentAdjustDetails(orderSubmitInfo);
            applyRentAdjustDetailsService.saveBatch(finRentAdjustDetailsArrayList, finRentAdjustDetailsArrayList.size());
            log.info("租金调整add成功！！！");


            /**  2.14解析影像文件ComAttachmentFile  **/
            ArrayList<ComAttachmentFile> comAttachmentFileArrayList = this.parseComAttachmentFile(orderSubmitInfo);
            comAttachmentFileService.saveBatch(comAttachmentFileArrayList,comAttachmentFileArrayList.size());
            log.info("影像文件add成功！！！");

            /**  2.15解析案件基本信息CaseBaseInfo  **/
            CaseBaseInfo caseBaseInfo = this.parseCaseBaseInfo(orderSubmitInfo, finCostInfoArrayList, caseCustInfoArrayList, caseCarInfoArrayList);
            // TODO 首次提交日期，退回再提交时候当前字段不做更新
            caseBaseInfo.setPassFirstDate(new Date());
            // TODO 总计时时效 给默认值
            caseBaseInfo.setEffectTimeReckon(0L);
            // 玄武进件调用默认未发送
            caseBaseInfo.setTortoiseApplyExecute(AfsEnumUtil.key(TortoiseApplyExecuteEnum.NOT_SEND));

            JSONArray arrayContact=new JSONArray();
            arrayContact.addAll(caseCustContactArrayList);
            jsonObject.put("caseContactList",arrayContact);
            log.info("客户联系方式冗余成功！！！");

            JSONArray carInfoArray=new JSONArray();
            carInfoArray.addAll(caseCarInfoArrayList);
            jsonObject.put("carInfoList",carInfoArray);
            log.info("车辆信息冗余成功！！！");


            JSONArray custAddressArray=new JSONArray();
            custAddressArray.addAll(caseCustAddressArrayList);
            jsonObject.put("custAddressList",custAddressArray);
            log.info("客户地址明细冗余成功！！！");

            JSONArray caseCustIndividualArray=new JSONArray();
            caseCustIndividualArray.addAll(caseCustIndividualArrayList);
            jsonObject.put("caseCustIndividualList",caseCustIndividualArray);
            log.info("个人客户明细信息冗余成功！！！");

            JSONArray caseCustInfoArray=new JSONArray();
            caseCustInfoArray.addAll(caseCustInfoArrayList);
            jsonObject.put("caseCustInfoList",caseCustInfoArray);
            log.info("客户信息冗余成功！！！");


            JSONArray arrayCost=new JSONArray();
            arrayCost.addAll(finCostInfoArrayList);
            object.put("finCostDetailsList",arrayCost);
            log.info("附条件融资信息冗余成功！！！");



            JSONArray arrayDiscount=new JSONArray();
            arrayDiscount.addAll(caseDiscountDetailArrayList);
            object.put("finDiscountDetailsList",arrayDiscount);
            log.info("附条件贴息冗余成功！！！");


            JSONArray arrayFinancing=new JSONArray();
            arrayFinancing.addAll(finFinancingItemsArrayList);
            object.put("finFinancingItemsList",arrayFinancing);
            log.info("附条件融资项冗余成功！！！");


            JSONArray arrayRentAdjust=new JSONArray();
            arrayRentAdjust.addAll(finRentAdjustDetailsArrayList);
            object.put("finRentAdjustDetailsList",arrayRentAdjust);
            log.info("附条件租金冗余成功！！！");

            //附条件核准冗余字段
            this.saveRedundantInfo(orderSubmitInfo,object,jsonObject);
            caseBaseInfoService.save(caseBaseInfo);

            log.info("------------------------------常规审批流程新增操作end----------------------------");

            /**  3.记录操作记录CaseApproveRecord  **/
            CaseApproveRecord caseApproveRecord = this.addCaseApproveRecord(orderSubmitInfo);
            caseApproveRecordService.save(caseApproveRecord);
            log.info("进件新增数据接收成功，申请编号：{}", caseBaseInfo.getApplyNo());
        }
        /**修改操作*/
        else if (OprTypeEnum.OPRUPDATE.getCode().equals(orderSubmitInfo.getOprType())
                ||OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(orderSubmitInfo.getOprType())) {
            log.info("------------------------------常规审批流程修改操作start----------------------------");
            /**  2.2解析客户联系方式CaseCustContact  **/
            ArrayList<CaseCustContact> caseCustContactArrayList = this.parseCaseCustContact(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(caseCustContactArrayList)) {
                caseCustContactService.saveBatch(caseCustContactArrayList, caseCustContactArrayList.size());
                log.info("客户联系方式add成功！！！");
            }

            /**  2.3解析车辆信息CaseCarInfo  **/
            ArrayList<CaseCarInfo> caseCarInfoArrayList = this.parseCaseCarInfo(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(caseCarInfoArrayList)) {
                caseCarInfoService.saveBatch(caseCarInfoArrayList, caseCarInfoArrayList.size());
                log.info("车辆信息update成功！！！");
            }

            /**  2.4解析融资信息CaseCostInfo  **/
            ArrayList<FinCostDetails> finCostInfoArrayList = this.parseCaseCostInfo(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(finCostInfoArrayList)) {
                caseCostInfoService.saveBatch(finCostInfoArrayList, finCostInfoArrayList.size());
                log.info("融资信息update成功！！！");
                JSONArray arrayCost=new JSONArray();
                arrayCost.addAll(finCostInfoArrayList);
                object.put("finCostDetailsList",arrayCost);
                log.info("附条件融资信息冗余成功！！！");
            }

            /**  2.5解析客户地址明细CaseCustAddress  **/
            ArrayList<CaseCustAddress> caseCustAddressArrayList = this.parseCaseCustAddress(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(caseCustAddressArrayList)) {
                caseCustAddressService.saveBatch(caseCustAddressArrayList, caseCustAddressArrayList.size());
                log.info("客户地址明细add成功！！！");
            }

            /**  2.6解析企业客户明细信息CaseCustCompany  **/
            ArrayList<CaseCustCompany> caseCustCompanyArrayList = this.parseCaseCustCompany(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(caseCustCompanyArrayList)) {
                caseCustCompanyService.updateBatchById(caseCustCompanyArrayList, caseCustCompanyArrayList.size());
                log.info("企业客户明细信息update成功！！！");
            }

            /**  2.7解析个人客户明细信息CaseCustIndividual  **/
            ArrayList<CaseCustIndividual> caseCustIndividualArrayList = this.parseCaseCustIndividual(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(caseCustIndividualArrayList)) {
                caseCustIndividualService.saveBatch(caseCustIndividualArrayList, caseCustIndividualArrayList.size());
                log.info("个人客户明细明细信息add成功！！！");
            }

            /**  2.8解析客户信息CaseCustInfo  **/
            ArrayList<CaseCustInfo> caseCustInfoArrayList = this.parseCaseCustInfo(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(caseCustInfoArrayList)) {
                for (CaseCustInfo custInfo : caseCustInfoArrayList) {
                    if(Objects.isNull(caseCustInfoService.getById(custInfo.getId()))){
                        caseCustInfoService.save(custInfo);
                        log.info("客户信息add成功！！！");
                    }else{
                        caseCustInfoService.updateById(custInfo);
                        log.info("客户信息update成功！！！");
                    }
                }
            }
            /**  2.9解析贴息明细信息CaseDiscountDetail  **/
            ArrayList<FinDiscountDetails> caseDiscountDetailArrayList = this.parseCaseDiscountDetail(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(caseDiscountDetailArrayList)) {
                caseDiscountDetailService.saveBatch(caseDiscountDetailArrayList, caseDiscountDetailArrayList.size());
                log.info("贴息明细add成功！！！");
                JSONArray arrayDiscount=new JSONArray();
                arrayDiscount.addAll(caseDiscountDetailArrayList);
                object.put("finDiscountDetailsList",arrayDiscount);
                log.info("附条件贴息冗余成功！！！");
            }

            /**  2.10解析融资项目信息CaseFinancingItems  **/
            ArrayList<FinFinancingItems> finFinancingItemsArrayList = this.parseCaseFinancingItems(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(finFinancingItemsArrayList)) {
                caseFinancingItemsService.saveBatch(finFinancingItemsArrayList, finFinancingItemsArrayList.size());
                log.info("融资项目信息add成功！！！");
                JSONArray arrayFinancing=new JSONArray();
                arrayFinancing.addAll(finFinancingItemsArrayList);
                object.put("finFinancingItemsList",arrayFinancing);
                log.info("附条件融资项冗余成功！！！");
            }

            /**  2.11解析案件渠道信息CaseChannelInfo  **/
            CaseChannelInfo caseChannelInfo = this.parseCaseChannelInfo(orderSubmitInfo);
            if (!ObjectUtils.isEmpty(caseChannelInfo)) {
                caseChannelInfoService.updateById(caseChannelInfo);
                log.info("案件渠道信息update成功！！！");
            }

            /**  2.12解析车辆信息CaseCarStyleDetail  **/
            ArrayList<CaseCarStyleDetail> caseCarStyleDetailArrayList = this.parseCaseCarStyleDetail(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(caseCarStyleDetailArrayList)) {
                caseCarStyleDetailService.updateBatchById(caseCarStyleDetailArrayList, caseCarStyleDetailArrayList.size());
                log.info("车辆信息update成功！！！");
            }

            /**  2.13解析租金调整ApplyRentAdjustDetails  **/
            ArrayList<FinRentAdjustDetails> finRentAdjustDetailsArrayList = this.parseApplyRentAdjustDetails(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(finRentAdjustDetailsArrayList)){
                applyRentAdjustDetailsService.updateBatchById(finRentAdjustDetailsArrayList, finRentAdjustDetailsArrayList.size());
                log.info("租金调整update成功！！！");
                JSONArray arrayRentAdjust=new JSONArray();
                arrayRentAdjust.addAll(finRentAdjustDetailsArrayList);
                object.put("finRentAdjustDetailsList",arrayRentAdjust);
                log.info("附条件租金冗余成功！！！");

            }

            /**  2.14解析影像文件ComAttachmentFile  **/
            ArrayList<ComAttachmentFile> comAttachmentFileArrayList = this.parseComAttachmentFile(orderSubmitInfo);
            if (CollectionUtil.isNotEmpty(comAttachmentFileArrayList)){
                comAttachmentFileService.saveOrUpdateBatch(comAttachmentFileArrayList,comAttachmentFileArrayList.size());
                log.info("影像资料update成功！！！");
            }

            JSONArray arrayContact=new JSONArray();
            arrayContact.addAll(caseCustContactArrayList);
            jsonObject.put("caseContactList",arrayContact);
            log.info("客户联系方式冗余成功！！！");

            JSONArray carInfoArray=new JSONArray();
            carInfoArray.addAll(caseCarInfoArrayList);
            jsonObject.put("carInfoList",carInfoArray);
            log.info("车辆信息冗余成功！！！");


            JSONArray custAddressArray=new JSONArray();
            custAddressArray.addAll(caseCustAddressArrayList);
            jsonObject.put("custAddressList",custAddressArray);
            log.info("客户地址明细冗余成功！！！");

            JSONArray caseCustIndividualArray=new JSONArray();
            caseCustIndividualArray.addAll(caseCustIndividualArrayList);
            jsonObject.put("caseCustIndividualList",caseCustIndividualArray);
            log.info("个人客户明细信息冗余成功！！！");

            JSONArray caseCustInfoArray=new JSONArray();
            caseCustInfoArray.addAll(caseCustInfoArrayList);
            jsonObject.put("caseCustInfoList",caseCustInfoArray);
            log.info("客户信息冗余成功！！！");

            //附条件核准冗余字段
            this.saveRedundantInfo(orderSubmitInfo,object,jsonObject);
            /**  2.15解析案件基本信息CaseBaseInfo  **/
            CaseBaseInfo caseBaseInfo = this.parseCaseBaseInfoForUpdate(orderSubmitInfo, finCostInfoArrayList, caseCustInfoArrayList,
                    caseCarInfoArrayList,orderSubmitInfo.getApplyScene());
            caseBaseInfoService.updateById(caseBaseInfo);

            log.info("------------------------------常规审批流程修改操作end----------------------------");
        }

    }

    /**
     * parse1CaseBaseInfo
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return CaseBaseInfo
     */
    private CaseBaseInfo parseCaseBaseInfo(OrderSubmitInfo orderSubmitInfo, List<FinCostDetails> finCostInfoArrayList, List<CaseCustInfo> caseCustInfoArrayList, List<CaseCarInfo> caseCarInfoArrayList) {
        if (ObjectUtils.isEmpty(orderSubmitInfo.getCaseBaseInfo())) {
            throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",caseBaseInfo不能为空");
        } else {
            if (ObjectUtils.isEmpty(orderSubmitInfo.getCaseBaseInfo().getApplyNo())) {
                throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",案件基本信息,applyNo不能为空");
            }
        }
        /**冗余经销商名称*/
        CaseBaseInfo caseBaseInfo = new CaseBaseInfo();
        BeanUtils.copyProperties(orderSubmitInfo.getCaseBaseInfo(), caseBaseInfo);
        CaseChannelInfoDto caseChannelInfo= orderSubmitInfo.getCaseChannelInfo();
        caseBaseInfo.setDealerName(caseChannelInfo.getDealerName());

        BigDecimal loanAmt=BigDecimal.ZERO;
        if(!CollectionUtils.isEmpty(finCostInfoArrayList)){
            for(FinCostDetails finCostDetails:finCostInfoArrayList){
                loanAmt=loanAmt.add(finCostDetails.getLoanAmt());
                if (CostTypeEnum.CARAMT.getCode().equals(finCostDetails.getCostType())) {
                    caseBaseInfo.setDownPayScaleRepeat(finCostDetails.getDownPayScale());
                    caseBaseInfo.setLoanTermsRepeat(finCostDetails.getLoanTerm());
                }
            }
            caseBaseInfo.setLoanAmtRepeat(loanAmt);
        }
        if (!CollectionUtils.isEmpty(caseCustInfoArrayList)) {
            caseCustInfoArrayList.forEach(caseCustInfo -> {
                if (CustRoleEnum.MIANCUST.getCode().equals(caseCustInfo.getCustRole())) {
                    caseBaseInfo.setCustNameRepeat(caseCustInfo.getCustName());
                    caseBaseInfo.setCertNoRepeat(caseCustInfo.getCertNo());
                } else {
                    caseBaseInfo.setBondManName(caseCustInfo.getCustName());
                }
            });
        }
        if (!CollectionUtils.isEmpty(caseCarInfoArrayList)) {
            caseCarInfoArrayList.forEach(caseCarInfo -> {
                caseBaseInfo.setSalePrice(caseCarInfo.getSalePrice());
            });
        }
        /**
         *置顶给默认值
         */
        caseBaseInfo.setIsStick(WhetherEnum.NO.getCode());
        //业务状态 已提交
        caseBaseInfo.setBusinessStateIn(AfsEnumUtil.key(BusinessStateInEnum.SUBMITTED));
        return caseBaseInfo;
    }

    /**
     * @description: 基础主表更新
     * @author jiaqi.guo
     * @created 2020/7/7 18:20
     * @version 1.0
     */
    private CaseBaseInfo parseCaseBaseInfoForUpdate(OrderSubmitInfo orderSubmitInfo, List<FinCostDetails> finCostInfoArrayList,
                                                    List<CaseCustInfo> caseCustInfoArrayList, List<CaseCarInfo> caseCarInfoArrayList,String applyScence) {
        if (ObjectUtils.isEmpty(orderSubmitInfo.getCaseBaseInfo())) {
            throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",caseBaseInfo不能为空");
        } else {
            if (ObjectUtils.isEmpty(orderSubmitInfo.getCaseBaseInfo().getApplyNo())) {
                throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",案件基本信息,applyNo不能为空");
            }
        }
        CaseBaseInfo caseBaseInfo = caseBaseInfoService.getOne(Wrappers.<CaseBaseInfo>query().lambda()
                .eq(CaseBaseInfo::getApplyNo, orderSubmitInfo.getApplyNo()));
        /**
         * 修订回复时累加贷额不做更新
         */
        BigDecimal totalLoanAmt=caseBaseInfo.getTotalLoanAmt();
        BeanUtils.copyProperties(orderSubmitInfo.getCaseBaseInfo(), caseBaseInfo);
        caseBaseInfo.setTotalLoanAmt(totalLoanAmt);
        BigDecimal loanAmt=BigDecimal.ZERO;
        if(!CollectionUtils.isEmpty(finCostInfoArrayList)){
            for(FinCostDetails finCostDetails:finCostInfoArrayList){
                loanAmt=loanAmt.add(finCostDetails.getLoanAmt());
                if (CostTypeEnum.CARAMT.getCode().equals(finCostDetails.getCostType())) {
                    caseBaseInfo.setDownPayScaleRepeat(finCostDetails.getDownPayScale());
                    caseBaseInfo.setLoanTermsRepeat(finCostDetails.getLoanTerm());
                }
            }
        }
        caseBaseInfo.setLoanAmtRepeat(loanAmt);
        if (!CollectionUtils.isEmpty(caseCustInfoArrayList)) {
            caseCustInfoArrayList.forEach(caseCustInfo -> {
                if (CustRoleEnum.MIANCUST.getCode().equals(caseCustInfo.getCustRole())) {
                    caseBaseInfo.setCustNameRepeat(caseCustInfo.getCustName());
                    caseBaseInfo.setCertNoRepeat(caseCustInfo.getCertNo());
                } else {
                    caseBaseInfo.setBondManName(caseCustInfo.getCustName());
                }
            });
        }
        if (!CollectionUtils.isEmpty(caseCarInfoArrayList)) {
            caseCarInfoArrayList.forEach(caseCarInfo -> {
                caseBaseInfo.setSalePrice(caseCarInfo.getSalePrice());
            });
        }
        //判断是否为经销商撤回后重新提交
        if(ApplySceneEnum.GENERALAPPROVAL.getCode().equals(applyScence)
                &&(AfsEnumUtil.key(BusinessStateInEnum.CALL_BACK_WAIT_SUBMIT).equals(caseBaseInfo.getBusinessStateIn())
                    ||(!AfsEnumUtil.key(BusinessStateInEnum.REVISE_PARSE).equals(caseBaseInfo.getBusinessStateIn())))){
            List<String> normalWorkflowTypes = new ArrayList<>();
            normalWorkflowTypes.add(WorkflowType.NORMAL_NEW.getAfsFlowKey());
            normalWorkflowTypes.add(WorkflowType.NORMAL_OLD.getAfsFlowKey());
            List<WorkProcessScheduleInfo> workProcessScheduleList = workProcessScheduleInfoService.list(
                    Wrappers.<WorkProcessScheduleInfo>lambdaQuery()
                            .in(WorkProcessScheduleInfo::getAfsFlowKey,normalWorkflowTypes)
                            .eq(WorkProcessScheduleInfo::getApplyNo,caseBaseInfo.getApplyNo())
                            .orderByDesc(WorkProcessScheduleInfo::getCreateTime));
            CaseApproveRecord record = new CaseApproveRecord();
            record.setApplyNo(caseBaseInfo.getApplyNo());
            if(CollectionUtil.isNotEmpty(workProcessScheduleList)){
                record.setStageId(workProcessScheduleList.get(0).getStageId());
            }
            record.setUseScene(UseSceneEnum.APPROVE.getValue());
            record.setApproveSuggest(AfsEnumUtil.key(LoanSubmitEnum.SUBMIT));
            record.setApproveSuggestName("经销商重新提交");
            record.setApproveEndTime(new Date());
            record.setApproveType(ApproveTypeEnum.PROCESS.getValue());
            record.setApproveRemark("【经销商重新提交】");
            caseApproveRecordService.save(record);
        }
        //业务状态 已提交
        caseBaseInfo.setBusinessStateIn(AfsEnumUtil.key(BusinessStateInEnum.SUBMITTED));
        return caseBaseInfo;
    }

    /**
     * parse1CaseBaseInfo
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return CaseChannelInfo
     */
    private CaseChannelInfo parseCaseChannelInfo(OrderSubmitInfo orderSubmitInfo) {
        if (ObjectUtils.isEmpty(orderSubmitInfo.getCaseChannelInfo())) {
            throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",caseChannelInfo不能为空");
        } else {
            if (ObjectUtils.isEmpty(orderSubmitInfo.getCaseChannelInfo().getApplyNo())) {
                throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",案件渠道信息,applyNo不能为空");
            }
        }
        CaseChannelInfo caseChannelInfo = new CaseChannelInfo();
        if (OprTypeEnum.OPRADD.getCode().equals(orderSubmitInfo.getOprType())) {
            BeanUtils.copyProperties(orderSubmitInfo.getCaseChannelInfo(), caseChannelInfo);
        }
        else if (OprTypeEnum.OPRUPDATE.getCode().equals(orderSubmitInfo.getOprType())
                ||OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(orderSubmitInfo.getOprType())) {
            caseChannelInfo =caseChannelInfoService.getOne(Wrappers.<CaseChannelInfo>query().lambda()
                    .eq(CaseChannelInfo::getApplyNo, orderSubmitInfo.getApplyNo()));
            BeanUtils.copyProperties(orderSubmitInfo.getCaseChannelInfo(), caseChannelInfo);
        }
        return caseChannelInfo;
    }

    /**
     * parseCaseCustContact
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return ArrayList<CaseCustContact>
     */
    private ArrayList<CaseCustContact> parseCaseCustContact(OrderSubmitInfo orderSubmitInfo) {

        ArrayList<CaseCustContact> caseCustContactArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getCaesCustContactList())) {
            //新增
            if (OprTypeEnum.OPRADD.getCode().equals(orderSubmitInfo.getOprType())) {
                for (CaseCustContactDto caseCustContactDto : orderSubmitInfo.getCaesCustContactList()) {
                    if (ObjectUtils.isEmpty(caseCustContactDto.getApplyNo()) || ObjectUtils.isEmpty(caseCustContactDto.getCustId())) {
                        throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",客户联系人信息,applyNo、custId不能为空");
                    }
                    CaseCustContact caseCustContact = new CaseCustContact();
                    BeanUtils.copyProperties(caseCustContactDto, caseCustContact);
                    caseCustContact.setValidStatus(ValidStatusEnum.EFFECTIVE.getCode());

                    caseCustContactArrayList.add(caseCustContact);
                }
            }
            //修改
            else if (OprTypeEnum.OPRUPDATE.getCode().equals(orderSubmitInfo.getOprType())
                    ||OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(orderSubmitInfo.getOprType())) {
                caseCustContactService.deleteByApplyNo(orderSubmitInfo.getApplyNo());
                for (CaseCustContactDto caseCustContactDto : orderSubmitInfo.getCaesCustContactList()) {
                    if (ObjectUtils.isEmpty(caseCustContactDto.getApplyNo()) || ObjectUtils.isEmpty(caseCustContactDto.getCustId())) {
                        throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",客户联系人信息,applyNo、custId不能为空");
                    }
                    CaseCustContact caseCustContact = new CaseCustContact();
                    BeanUtils.copyProperties(caseCustContactDto, caseCustContact);
                    caseCustContact.setValidStatus(ValidStatusEnum.EFFECTIVE.getCode());

                    caseCustContactArrayList.add(caseCustContact);
                }
            }
        }
        return caseCustContactArrayList;
    }

    /**
     * addCaseApproveRecord
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return CaseApproveRecord
     */
    private CaseApproveRecord addCaseApproveRecord(OrderSubmitInfo orderSubmitInfo) {

        CaseApproveRecord caseApproveRecord = new CaseApproveRecord();
        caseApproveRecord.setApplyNo(orderSubmitInfo.getCaseBaseInfo().getApplyNo());
        caseApproveRecord.setUseScene(UseSceneEnum.APPROVE.getValue());
        caseApproveRecord.setApproveRemark("进件子系统提交案件信息");
        caseApproveRecord.setApproveStartTime(Calendar.getInstance().getTime());
        caseApproveRecord.setApproveEndTime(Calendar.getInstance().getTime());
        return caseApproveRecord;
    }

    /**
     * parseCaseCarInfo
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return ArrayList<CaseCarInfo>
     */
    private ArrayList<CaseCarInfo> parseCaseCarInfo(OrderSubmitInfo orderSubmitInfo) throws ParseException {
        ArrayList<CaseCarInfo> caseCarInfoArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getCaseCarInfoList())) {
            if (OprTypeEnum.OPRADD.getCode().equals(orderSubmitInfo.getOprType())) {
                for (CaseCarInfoDto caseCarInfoDto : orderSubmitInfo.getCaseCarInfoList()) {
                    if (ObjectUtils.isEmpty(caseCarInfoDto.getId()) || ObjectUtils.isEmpty(caseCarInfoDto.getApplyNo())) {
                        throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",车辆信息,id、applyNo不能为空");
                    }
                    CaseCarInfo caseCarInfo = new CaseCarInfo();
                    BeanUtils.copyProperties(caseCarInfoDto, caseCarInfo);
                    caseCarInfo.setId(caseCarInfoDto.getId());
                    caseCarInfoArrayList.add(caseCarInfo);
                }

            } else if (OprTypeEnum.OPRUPDATE.getCode().equals(orderSubmitInfo.getOprType())
                    ||OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(orderSubmitInfo.getOprType())) {
                caseCarInfoService.remove(Wrappers.<CaseCarInfo>query().lambda()
                        .eq(CaseCarInfo::getApplyNo, orderSubmitInfo.getApplyNo()));
                for (CaseCarInfoDto caseCarInfoDto : orderSubmitInfo.getCaseCarInfoList()) {
                    if (ObjectUtils.isEmpty(caseCarInfoDto.getId()) || ObjectUtils.isEmpty(caseCarInfoDto.getApplyNo())) {
                        throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",车辆信息,id、applyNo不能为空");
                    }
                    CaseCarInfo caseCarInfo = new CaseCarInfo();
                    BeanUtils.copyProperties(caseCarInfoDto, caseCarInfo);
                    caseCarInfo.setId(caseCarInfoDto.getId());
                    caseCarInfoArrayList.add(caseCarInfo);
                }
            }

        } else {
            throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",caseCarInfoList不可为空");
        }
        return caseCarInfoArrayList;
    }

    /**
     * parseCaseCarStyleDetail
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return ArrayList<CaseCarStyleDetail>
     */
    private ArrayList<CaseCarStyleDetail> parseCaseCarStyleDetail(OrderSubmitInfo orderSubmitInfo) {

        ArrayList<CaseCarStyleDetail> caseCarStyleDetailArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getCaseCarStyleDetailList())) {
            for (CaseCarStyleDetailDto caseCarStyleDetailDto : orderSubmitInfo.getCaseCarStyleDetailList()) {
                CaseCarStyleDetail caseCarStyleDetail = new CaseCarStyleDetail();
                BeanUtils.copyProperties(caseCarStyleDetailDto, caseCarStyleDetail);
                caseCarStyleDetailArrayList.add(caseCarStyleDetail);
            }
        }
        return caseCarStyleDetailArrayList;
    }

    /**
     * 影像资料解析
     * @param orderSubmitInfo
     * @return
     */
    private ArrayList<ComAttachmentFile> parseComAttachmentFile(OrderSubmitInfo orderSubmitInfo){
        ArrayList<ComAttachmentFile> comAttachmentFileArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getComAttachmentFileListDto())) {
            for (ComAttachmentFileDto attachmentFileDto : orderSubmitInfo.getComAttachmentFileListDto()) {
                if (ObjectUtils.isEmpty(attachmentFileDto.getBusiNo()) || ObjectUtils.isEmpty(attachmentFileDto.getBelongNo())) {
                    throw new AfsBaseException("业务编号:"+attachmentFileDto.getBusiNo() + ",所属业务编号:" + attachmentFileDto.getBelongNo()+"不能为空");
                }
                ComAttachmentFile comAttachmentFile = new ComAttachmentFile();
                BeanUtils.copyProperties(attachmentFileDto,comAttachmentFile);
                comAttachmentFileArrayList.add(comAttachmentFile);
            }
        }
        return comAttachmentFileArrayList;
    }
    /**
     * <p>Description: </p>
     * @param orderSubmitInfo
     * @return ArrayList<ApplyRentAdjustDetails>
     */
    private ArrayList<FinRentAdjustDetails> parseApplyRentAdjustDetails(OrderSubmitInfo orderSubmitInfo) {
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getApplyRentAdjustDetailsList())) {
            if (OprTypeEnum.OPRUPDATE.getCode().equals(orderSubmitInfo.getOprType())
                    ||OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(orderSubmitInfo.getOprType())) {
                rentAdjustDetailsService.deleteByApplyNo(orderSubmitInfo.getApplyNo());
            }
        }
        ArrayList<FinRentAdjustDetails> adjustDetailsArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getApplyRentAdjustDetailsList())) {
            for (FinRentAdjustDetails finRentAdjustDetailsDto : orderSubmitInfo.getApplyRentAdjustDetailsList()) {
                FinRentAdjustDetails finRentAdjustDetails = new FinRentAdjustDetails();
                BeanUtils.copyProperties(finRentAdjustDetailsDto, finRentAdjustDetails);
                adjustDetailsArrayList.add(finRentAdjustDetails);
            }
        }
        return adjustDetailsArrayList;
    }

    /**
     * parseCaseCostInfo
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return ArrayList<CaseCostInfo>
     */
    private ArrayList<FinCostDetails> parseCaseCostInfo(OrderSubmitInfo orderSubmitInfo) {

        ArrayList<FinCostDetails> finCostInfoArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getCaseCostInfoList())) {
            //新增
            if (OprTypeEnum.OPRADD.getCode().equals(orderSubmitInfo.getOprType())) {
                for (FinCostDetails finCostInfoDto : orderSubmitInfo.getCaseCostInfoList()) {
                    if (ObjectUtils.isEmpty(finCostInfoDto.getId()) || ObjectUtils.isEmpty(finCostInfoDto.getApplyNo())) {
                        throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",融资信息，id、applyNo");
                    }
                    FinCostDetails finCostInfo = new FinCostDetails();
                    BeanUtils.copyProperties(finCostInfoDto, finCostInfo);
                    finCostInfoArrayList.add(finCostInfo);
                }
            }
            else if (OprTypeEnum.OPRUPDATE.getCode().equals(orderSubmitInfo.getOprType())
                    ||OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(orderSubmitInfo.getOprType())) {
                caseCostInfoService.deleteByApplyNo(orderSubmitInfo.getApplyNo());
                for (FinCostDetails finCostInfoDto : orderSubmitInfo.getCaseCostInfoList()) {
                    if (ObjectUtils.isEmpty(finCostInfoDto.getId()) || ObjectUtils.isEmpty(finCostInfoDto.getApplyNo())) {
                        throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",融资信息，id、applyNo");
                    }
                    FinCostDetails finCostInfo = new FinCostDetails();
                    BeanUtils.copyProperties(finCostInfoDto, finCostInfo);
                    finCostInfoArrayList.add(finCostInfo);
                }
            }


        } else {
            log.info("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",caseCostInfoList不可为空！");
            throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",caseCostInfoList不可为空！");
        }
        return finCostInfoArrayList;
    }

    /**
     * parseCaseCustAddress
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return ArrayList<CaseCustAddress>
     */
    private ArrayList<CaseCustAddress> parseCaseCustAddress(OrderSubmitInfo orderSubmitInfo) {

        ArrayList<CaseCustAddress> caseCustAddressArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getCaseCustAddressList())) {
            //新增
            if (OprTypeEnum.OPRADD.getCode().equals(orderSubmitInfo.getOprType())) {
                for (CaseCustAddressDto caseCustAddressDto : orderSubmitInfo.getCaseCustAddressList()) {
                    if (ObjectUtils.isEmpty(caseCustAddressDto.getApplyNo()) || ObjectUtils.isEmpty(caseCustAddressDto.getCustId())) {
                        throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",客户地址信息,applyNo、custId不能为空");
                    }
                    CaseCustAddress caseCustAddress = new CaseCustAddress();
                    BeanUtils.copyProperties(caseCustAddressDto, caseCustAddress);
                    caseCustAddress.setValidStatus(ValidStatusEnum.EFFECTIVE.getCode());
                    caseCustAddressArrayList.add(caseCustAddress);
                }
            }
            else if (OprTypeEnum.OPRUPDATE.getCode().equals(orderSubmitInfo.getOprType())
                    ||OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(orderSubmitInfo.getOprType())) {
                caseCustAddressService.remove(Wrappers.<CaseCustAddress>query().lambda()
                        .eq(CaseCustAddress::getApplyNo, orderSubmitInfo.getApplyNo()));
                for (CaseCustAddressDto caseCustAddressDto : orderSubmitInfo.getCaseCustAddressList()) {
                    if (ObjectUtils.isEmpty(caseCustAddressDto.getApplyNo()) || ObjectUtils.isEmpty(caseCustAddressDto.getCustId())) {
                        throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",客户地址信息,applyNo、custId不能为空");
                    }
                    CaseCustAddress caseCustAddress = new CaseCustAddress();
                    BeanUtils.copyProperties(caseCustAddressDto, caseCustAddress);
                    caseCustAddress.setValidStatus(ValidStatusEnum.EFFECTIVE.getCode());
                    caseCustAddressArrayList.add(caseCustAddress);
                }
            }
        }
        return caseCustAddressArrayList;
    }

    /**
     * parseCaseCustCompany
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return ArrayList<CaseCustCompany>
     */
    private ArrayList<CaseCustCompany> parseCaseCustCompany(OrderSubmitInfo orderSubmitInfo) throws ParseException {

        ArrayList<CaseCustCompany> caseCustCompanyArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getCaseCustCompanyList())) {
            for (CaseCustCompanyDto caseCustCompanyDto : orderSubmitInfo.getCaseCustCompanyList()) {
                if (ObjectUtils.isEmpty(caseCustCompanyDto.getCustId())) {
                    throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",企业客户信息，custId不能为空");
                }
                CaseCustCompany caseCustCompany = new CaseCustCompany();
                BeanUtils.copyProperties(caseCustCompanyDto, caseCustCompany);
                caseCustCompanyArrayList.add(caseCustCompany);
            }
        }
        return caseCustCompanyArrayList;
    }

    /**
     * parseCaseCustIndividual
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return ArrayList<CaseCustIndividual>
     */
    private ArrayList<CaseCustIndividual> parseCaseCustIndividual(OrderSubmitInfo orderSubmitInfo) throws ParseException {

        ArrayList<CaseCustIndividual> caseCustIndividualArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getCaseCustIndividualList())) {
            if (OprTypeEnum.OPRADD.getCode().equals(orderSubmitInfo.getOprType())) {
                for (CaseCustIndividualDto caseCustIndividualDto : orderSubmitInfo.getCaseCustIndividualList()) {
                    if (ObjectUtils.isEmpty(caseCustIndividualDto.getCustId())) {
                        throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",个人客户信息，custId不能为空");
                    }
                    CaseCustIndividual caseCustIndividual = new CaseCustIndividual();
                    BeanUtils.copyProperties(caseCustIndividualDto, caseCustIndividual);
                    caseCustIndividualArrayList.add(caseCustIndividual);
                }
            }
            else if (OprTypeEnum.OPRUPDATE.getCode().equals(orderSubmitInfo.getOprType())
                    ||OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(orderSubmitInfo.getOprType())) {
                for (CaseCustIndividualDto caseCustIndividualDto : orderSubmitInfo.getCaseCustIndividualList()) {
                    if (ObjectUtils.isEmpty(caseCustIndividualDto.getCustId())) {
                        throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",个人客户信息，custId不能为空");
                    }
                    caseCustIndividualService.remove(Wrappers.<CaseCustIndividual>query().lambda()
                            .eq(CaseCustIndividual::getCustId, caseCustIndividualDto.getCustId()));
                    CaseCustIndividual caseCustIndividual = new CaseCustIndividual();
                    BeanUtils.copyProperties(caseCustIndividualDto, caseCustIndividual);
                    caseCustIndividualArrayList.add(caseCustIndividual);
                }
            }
        }
        return caseCustIndividualArrayList;
    }

    /**
     * parseCaseCustInfo
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return ArrayList<CaseCustInfo>
     */
    private ArrayList<CaseCustInfo> parseCaseCustInfo(OrderSubmitInfo orderSubmitInfo) {

        ArrayList<CaseCustInfo> caseCustInfoArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getCaseCustInfoList()) && orderSubmitInfo.getCaseCustInfoList().size() > 0) {
            for (CaseCustInfoDto caseCustInfoDto : orderSubmitInfo.getCaseCustInfoList()) {
                if (ObjectUtils.isEmpty(caseCustInfoDto.getId()) || ObjectUtils.isEmpty(caseCustInfoDto.getApplyNo())) {
                    log.info("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",客户信息,id、applyNo不能为空");
                    throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",客户信息,id、applyNo不能为空");
                }
                CaseCustInfo caseCustInfo = new CaseCustInfo();
                BeanUtils.copyProperties(caseCustInfoDto, caseCustInfo);
                caseCustInfoArrayList.add(caseCustInfo);
            }
        } else {
            throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",caseCustInfoList不能为空");
        }
        return caseCustInfoArrayList;
    }

    /**
     * parseCaseDiscountDetail
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return ArrayList<CaseDiscountDetail>
     */
    private ArrayList<FinDiscountDetails> parseCaseDiscountDetail(OrderSubmitInfo orderSubmitInfo) {

        ArrayList<FinDiscountDetails> caseDiscountDetailArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getCaseDiscountDetailList()) && orderSubmitInfo.getCaseDiscountDetailList().size() > 0) {
            if (OprTypeEnum.OPRUPDATE.getCode().equals(orderSubmitInfo.getOprType())
                    ||OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(orderSubmitInfo.getOprType())) {
                caseDiscountDetailService.deleteByApplyNo(orderSubmitInfo.getCaseDiscountDetailList().get(0).getApplyNo());
            }
            for (FinDiscountDetails finDiscountDetailsDto : orderSubmitInfo.getCaseDiscountDetailList()) {
                if (ObjectUtils.isEmpty(finDiscountDetailsDto.getApplyNo()) || ObjectUtils.isEmpty(finDiscountDetailsDto.getCostId())) {
                    throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",贴息信息，applyNo和costId不能为空");
                }
                FinDiscountDetails caseDiscountDetail = new FinDiscountDetails();
                BeanUtils.copyProperties(finDiscountDetailsDto, caseDiscountDetail);
                caseDiscountDetailArrayList.add(caseDiscountDetail);
            }
        }
        return caseDiscountDetailArrayList;
    }

    /**
     * parseCaseFinancingItems
     * <p>Description: </p>
     *
     * @param orderSubmitInfo orderSubmitInfo
     * @return ArrayList<CaseFinancingItems>
     */
    private ArrayList<FinFinancingItems> parseCaseFinancingItems(OrderSubmitInfo orderSubmitInfo) {

        ArrayList<FinFinancingItems> finFinancingItemsArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderSubmitInfo.getCaseFinancingItemList()) && orderSubmitInfo.getCaseFinancingItemList().size() > 0) {
            if (OprTypeEnum.OPRUPDATE.getCode().equals(orderSubmitInfo.getOprType())
                    ||OprTypeEnum.UPDATE_RECONSIDER.getCode().equals(orderSubmitInfo.getOprType())) {
                caseFinancingItemsService.deleteByApplyNo(orderSubmitInfo.getCaseFinancingItemList().get(0).getApplyNo());
            }
            for (FinFinancingItems finFinancingItemsDto : orderSubmitInfo.getCaseFinancingItemList()) {
                if (ObjectUtils.isEmpty(finFinancingItemsDto.getId()) || ObjectUtils.isEmpty(finFinancingItemsDto.getApplyNo())) {
                    log.info("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",融资项目信息，id、applyNo不能为空");
                    throw new AfsBaseException("申请编号:" + orderSubmitInfo.getCaseBaseInfo().getApplyNo() + ",融资项目信息，id、applyNo不能为空");
                }
                FinFinancingItems finFinancingItems = new FinFinancingItems();
                BeanUtils.copyProperties(finFinancingItemsDto, finFinancingItems);
                finFinancingItemsArrayList.add(finFinancingItems);
            }
        }

        return finFinancingItemsArrayList;
    }
    /**
     * @description: 数据冗余
     * @author jiaqi.guo
     * @created 2020/8/15 14:57
     * @version 1.0
     */
    private void saveRedundantInfo(OrderSubmitInfo submitInfo,JSONObject object,JSONObject jsonObject){
        CaseRedundantInfo redundantInfo= redundantInfoService.getOne(Wrappers.<CaseRedundantInfo>query().lambda()
                .eq(StringUtils.isNotBlank(submitInfo.getApplyNo()),CaseRedundantInfo::getApplyNo,submitInfo.getApplyNo()));

        //常规审批
        if(ApplySceneEnum.GENERALAPPROVAL.getCode().equals(submitInfo.getApplyScene())){
           if(OprTypeEnum.OPRADD.getCode().equals(submitInfo.getOprType())){
                   CaseRedundantInfo caseRedundantInfo= new CaseRedundantInfo();
                   caseRedundantInfo.setBackSign(WhetherEnum.NO.getCode());
                   caseRedundantInfo.setApplyNo(submitInfo.getApplyNo());
                   caseRedundantInfo.setReconsiderSign(WhetherEnum.NO.getCode());
                   caseRedundantInfo.setBackEvidence(jsonObject.toString());
                   caseRedundantInfo.setConditionalApproval(object.toString());
                   redundantInfoService.save(caseRedundantInfo);
           }else{
               if(!ObjectUtils.isEmpty(redundantInfo)){
                   redundantInfo.setBackSign(WhetherEnum.YES.getCode());
                   redundantInfo.setReconsiderSign(WhetherEnum.NO.getCode());
                   redundantInfo.setConditionalApproval(object.toString());
                   redundantInfoService.updateById(redundantInfo);
               }
           }
        }

        //正式复议
        if(ApplySceneEnum.FORMALREVIEW.getCode().equals(submitInfo.getApplyScene())){
            if(OprTypeEnum.OPRUPDATE.getCode().equals(submitInfo.getOprType())){
                if(!ObjectUtils.isEmpty(redundantInfo)){
                    redundantInfo.setReconsiderEvidence(jsonObject.toString());
                    redundantInfo.setConditionalApproval(object.toString());
                    redundantInfoService.updateById(redundantInfo);
                }
            }else{
                if(!ObjectUtils.isEmpty(redundantInfo)){
                    redundantInfo.setBackSign(WhetherEnum.NO.getCode());
                    redundantInfo.setReconsiderSign(WhetherEnum.YES.getCode());
                    redundantInfo.setConditionalApproval(object.toString());
                    redundantInfoService.updateById(redundantInfo);
                }
            }
        }
    }
}
